﻿//Example of use only. Unnecessary for use in other projects.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace AutoScrollViewer
{
    public partial class Mover : UserControl
    {
        private Point _startingDragPoint;

        public Mover()
        {
            InitializeComponent();
            MouseLeftButtonDown += Rectangle_MouseLeftButtonDown;
        }


        private void Rectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement DragObject = (FrameworkElement)sender;
            _startingDragPoint = e.GetPosition(DragObject);
            DragObject.CaptureMouse();
            DragObject.MouseMove += new MouseEventHandler(Rectangle_MouseMove);
            DragObject.MouseLeftButtonUp += new MouseButtonEventHandler(Rectangle_MouseLeftButtonUp);
        }



        private void Rectangle_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement DragObject = (FrameworkElement)sender;
            DragObject.ReleaseMouseCapture();

            DragObject.MouseMove -= Rectangle_MouseMove;
            DragObject.MouseLeftButtonUp -= Rectangle_MouseLeftButtonUp;
        }

        private void Rectangle_MouseMove(object sender, MouseEventArgs e)
        {
            Canvas Canvas = (Canvas)this.Parent;
            Point Point = e.GetPosition(Canvas);
            FrameworkElement DragObject = (FrameworkElement)sender;
            Canvas.SetLeft(DragObject, Point.X - _startingDragPoint.X);
            Canvas.SetTop(DragObject, Point.Y - _startingDragPoint.Y);
        }
    }
}
